package com.zlz.blog.server.config.login;

import com.zlz.blog.manage.common.entity.oauth.LoginResult;
import com.zlz.blog.manage.common.entity.oauth.LoginUser;
import com.zlz.blog.manage.common.response.QRCodeResp;
import com.zlz.blog.manage.common.response.ResultSet;
import com.zlz.blog.server.oauth.service.LoginUserService;
import com.zlz.blog.server.oauth.service.WeChatService;
import com.zlz.blog.server.util.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wb_zhulinzhong
 * @date 2021-05-19 19:33:19
 */
@RestController
public class LoginController {

    private LoginUserService loginUserService;
    private WeChatService weChatService;

    @Autowired
    public void setLoginUserService(LoginUserService loginUserService) {
        this.loginUserService = loginUserService;
    }
    @Autowired
    public void setWeChatService(WeChatService weChatService) {
        this.weChatService = weChatService;
    }

    @Resource
    private LoginUtil loginUtil;

    /**
     * 用户登录
     *
     * @param loginUser
     * @param type
     * @param request
     * @return
     */
    @PostMapping("/login/{type}")
    public ResultSet<LoginResult> login(@RequestBody LoginUser loginUser,
                                        @PathVariable("type") String type,
                                        HttpServletRequest request) {
        return loginUserService.login(request, loginUser, type);
    }

    /**
     * 用户注册
     *
     * @param loginUser
     * @param type
     * @param request
     * @return
     */
    @PostMapping("/register/{type}")
    public ResultSet<LoginResult> register(@RequestBody LoginUser loginUser,
                                           @PathVariable("type") String type,
                                           HttpServletRequest request) {
        return loginUserService.registerUser(request, loginUser, type);
    }

    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @GetMapping("/logout")
    public ResultSet<LoginResult> logOut(HttpServletRequest request) {
        loginUtil.logout(request);
        return ResultSet.success("退出登陆成功");
    }

    /**
     * 获取当前用户
     *
     * @return
     */
    @GetMapping("/currentuser")
    public ResultSet<LoginUser> currentUser() {
        return ResultSet.success(loginUtil.getLoginUser());
    }

    /**
     * 获取微信扫码登录二维码
     * @param response
     * @return
     */
    @GetMapping("/wechat/code")
    public QRCodeResp getWechatCode(HttpServletResponse response){
        return weChatService.getLoginQRCode(response);
    }
}
